package com.hotacorp.opencloud.authserver.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.authserver.service.SysCaptchaService;
import com.hotacorp.opencloud.common.commdata.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 登录处理，用于生成图片验证码和退出登录，图片验证码5分钟内有效
 * @author lwg
 *
 */
@RestController
@Api(tags = "登录辅助处理API")
public class LoginDealController {
	
	@Autowired
	private SysCaptchaService sysCaptchaService;
	
	@Autowired
	private TokenStore tokenStore;
	/**
	 * 验证码
	 */
	@GetMapping("/oauth/captcha.jpg")
	@ApiOperation(value = "生成图片")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "uuid", value = "随机唯一标识", required = true, dataType = "String"),
    })
	public void captcha(HttpServletResponse response, @RequestParam String uuid)throws IOException {
		response.setHeader("Cache-Control", "no-store, no-cache");
		response.setContentType("image/jpeg");

		//获取图片验证码
		BufferedImage image = sysCaptchaService.getCaptcha(uuid);

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		IOUtils.closeQuietly(out);
	}

    @PostMapping(value = "/oauth/logout")
	@ApiOperation(value = "退出登录")
    public Result<?> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader, @RequestParam(value = "access_token", required = false) String access_token){
    	if (StringUtils.isBlank(authHeader) && StringUtils.isBlank(access_token)) {
			return Result.failed("退出失败，token 为空");
		}

		String tokenValue = "";
		if (!StringUtils.isBlank(authHeader)) {
			tokenValue = authHeader.replace(OAuth2AccessToken.BEARER_TYPE, "").trim();
		} else {
			tokenValue = access_token;
		}
		OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
		if (accessToken != null) {
			tokenStore.removeAccessToken(accessToken);
			OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
			if (refreshToken != null) {
				tokenStore.removeRefreshToken(refreshToken);
			}
		}

		return Result.succeed();
    }

}
